import random

def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
    global flag                                                                                                           #These flags are used later as part of a rough way of identifying players.
    flag = 0
    hunt_decisions = []
    hunt_count = round_number * current_reputation * 100
    slack_count = (round_number * 100) - hunt_count
    global reps
    reps = player_reputations
    global round_info
    round_info = round_number

    for i in range(len(player_reputations)):
        if ((round_number != 1) and ((player_reputations[i] <=.2) or (player_decisions >= .9))):                          #prevents us from hunting against algorithms which almost always slack, and 
            hunt_decisions.append('s')                                                                                    #lets us slack when someone almost always hunts
        else:
            hunt_chance = random.randint(1,10)                                                                            #default: hunt 70% of the time
        if hunt_chance <= 7:
            hunt_decisions.append('h')
        else:
            hunt_decisions.append('s')
    if flag == 1:                                                                                                         #This code activates if our algorithm has found a player likely to slack against our code.                                                                                     
        for i in range(len(player_reputations)):                                                                          #If a player's reputation is within the range in which that this player's reputation can be 
            for j in range(len(slackers)):                                                                                #(calculated by us as reputation plus or minus reputation times one over round number)
                if ((player_reputations[i] >= (slackers[i][0] - (slackers[i][0] * (1/round_number)))) and (player_reputations[i] <= (slackers[i][0] + (slackers[i][0] * (1/round_number))))):
                    hunt_decisions.pop(i)                                                                                 #we do not hunt with them.
                    hunt_decisions.insert(i, 's')                                                           
    for i in range(len(hunt_decisions)):                                                                                  #This code is used to predict what our reputation will be at the end of the round and 
        if hunt_decisions[i] == 'h':                                                                                      #to keep it in the middle (between .4 and .7), so that algorithms based off of player reputations do not
            hunt_count += 1                                                                                               #make assumptions about our code. Doing this also helps us be "forgiving" and get out of slacking cycles
        elif hunt_decisions[i] == 's':                                                                                    #against code implementing a "tit for tat" type strategy.
            slack_count += 1
        expected_reputation = hunt_count/(hunt_count + slack_count)
        if expected_reputation < .4:
            hunt_decisions.pop(i)
            hunt_decisions.insert(i,'h')
        if expected_reputation > .7:
            hunt_decisions.pop(i)
            hunt_decisions.insert(i,'s')
    return hunt_decisions;

def hunt_outcomes(food_earnings):
    global tracking                                                                                                       #We used this function to create an array with players' actions towards us. It stores their reputation,   
    tracking = []                                                                                                         #our change in food after interacting with them, and during which round the interaction took place.
    for i in range(len(reps)):
        players_action = [reps[i], food_earnings[i], round_info]
        tracking.append(players_action)
   
def round_end(award, m, number_hunters):

    pass

def flag_slackers():                                                                                                     #Our team added this function in order to identify players who are likely to slack against our algorithm
    global slackers                                                                                                      #based off of our array of players' reputation, food earnings, and round number. If we lost food when
    slackers = []                                                                                                        #interacting with a player, they must have slacked. Their information is added to a list of slackers.
    for i in range(len(tracking)):
        if tracking[i][1] < 0:                                                                                           
            slackers.append(tracking[i])                                                                                 #We sort through the list of slackers to find a player that has slacked 2 or more times. We only keep
    for i in range(len(slackers)):                                                                                       #the most recent data from this player. We identify players as the "same" if their reputation has changed 
        for j in range(len(slackers)):                                                                                   #by less than its previous state times the inverse of the round number.
            if slackers[j][2] == slackers[i][2] + 1:                                    
                if ((slackers[j][0] >= (slackers[i][0] - (slackers[i][0] * (1/slackers[i][2])))) and (slackers[j][0] <= (slackers[i][0] + (slackers[i][0] * (1/slackers[i][2]))))):
                    slackers.pop(i)
                    flag = 1                                                                                             #If we have slackers, we put up a flag as indication.
                                                                                                                        
                                                                                                                          
    
        
